/*
 * @Author: qiuz
 * @Date: 2018-03-20 17:34:52
 * */

/*
 * 时间戳 => 日期格式
 * */

import LocalStorage from '../local-storage';
import injector from 'angular-es-utils/injector';

/* eslint-disable */
Date.prototype.format = function (format) {
	let args = {
		'M+': this.getMonth() + 1,
		'd+': this.getDate(),
		'h+': this.getHours(),
		'm+': this.getMinutes(),
		's+': this.getSeconds(),
		'q+': Math.floor((this.getMonth() + 3) / 3), // quarter
		'S': this.getMilliseconds()
	};
	if (/(y+)/.test(format)) { format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length)); }
	for (let i in args) {
		let n = args[i];
		if (new RegExp('(' + i + ')').test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ('00' + n).substr(('' + n).length)); }
	}
	return format;
};
/* eslint-disable */

const getZtreeString = function (array) {
	console.log(array);
	let tip = '';
	if (array.filter(item => item.type === 1).length === 34) {
		return tip = '全国';
	}
	let proCityObj = {};
	let resultArray = [];
	angular.forEach(array, function (data, index) {
		if (data.type === '1') {
			const code = data.code;
			proCityObj[code] = { pro: data.name, citys: [] };
		}
	});
	console.log(proCityObj);
	angular.forEach(array, function (data, index) {
		if (data.type === '2') {
			const pcode = data.pcode;
			proCityObj[pcode].citys.push(data.name);
		}
	});
	console.log(proCityObj);
	for (const key in proCityObj) {
		if (proCityObj.hasOwnProperty(key)) {
			let pro = proCityObj[key].pro;
			let citys = proCityObj[key].citys.join(',');
			if (pro === '上海' || pro === '北京' || pro === '天津' || pro === '重庆') {
				resultArray.push(`${pro}`);
			} else {
				resultArray.push(`${pro}(${citys})`);
			}
		}
	}
	console.log(resultArray);
	tip = resultArray.join(';');
	return tip;
}

const timestampToDate = function (time) {
	let date = new Date(time);
	if (!(date instanceof Date && !isNaN(date.getTime()))) {
		return '参数必须为时间戳(数字)';
	}
	const Y = date.getFullYear() + '-',
		M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-',
		D = date.getDate() + ' ',
		h = date.getHours() + ':',
		m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':',
		s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
	return Y + M + D + h + m + s;
};

/**
 * 返回上一页
 */
const goBack = () => {
	history.go(-1);
};

// 设置cookie
const setCookie = (c_name, value, expiredays) => {
	const exp = new Date();
	exp.setTime(exp.getTime() + expiredays * 24 * 60 * 60 * 1000);
	document.cookie = c_name + '=' + escape(value) + '' + ((expiredays == null) ? '' : ';expires=' + exp.toGMTString());
};

// 获取cookie
const getCookie = c_name => {
	if (document.cookie.length > 0) {
		let c_start = document.cookie.indexOf(c_name + '=');
		if (c_start !== -1) {
			c_start = c_start + c_name.length + 1;
			let c_end = document.cookie.indexOf(';', c_start);
			if (c_end === -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return '';
};

// 删除cookie
const delCookie = c_name => {
	const date = new Date();
	date.setTime(date.getTime() - 10000);
	document.cookie = c_name + '=a;expires=' + date.toGMTString();
};

// 每个功能页获取buttonList;
const getButtonList = url => {
	if (!url) {
		url = localStorage.nowActiveUrl;
	}
	const menuButtonList = LocalStorage.getItem('menuInfo').menuButtonList;
	const buttonArray = menuButtonList.filter(item => item.menuUrl === url);
	return (buttonArray.length > 0 && buttonArray[0].childButtonList) ? buttonArray[0].childButtonList : [];
};

// 订单状态转中文 1 未派单 2 派单中 3 未发车 4 已发车 5 已到达 6 服务中 7 已完成  8 已取消
const orderCheckStateTrans = state => {
	let chineseHtml = '';
	switch (state) {
		case '0':
			chineseHtml = '待审核';
			break;
		case '1':
			chineseHtml = '已审核';
			break;
		case '待审核':
			chineseHtml = '0';
			break;
		case '已审核':
			chineseHtml = '1';
	}
	return chineseHtml;
};

// 订单状态转中文 1 未派单 2 派单中 3 未发车 4 已发车 5 已到达 6 服务中 7 已完成  8 已取消
const orderStateTrans = state => {
	let chineseHtml = '';
	switch (state) {
		case '1':
			chineseHtml = '未派单';
			break;
		case '2':
			chineseHtml = '派单中';
			break;
		case '3':
			chineseHtml = '未发车';
			break;
		case '4':
			chineseHtml = '已发车';
			break;
		case '5':
			chineseHtml = '已到达';
			break;
		case '6':
			chineseHtml = '服务中';
			break;
		case '7':
			chineseHtml = '已完成';
			break;
		case '8':
			chineseHtml = '已取消';
			break;
		case '未派单':
			chineseHtml = '1';
			break;
		case '派单中':
			chineseHtml = '2';
			break;
		case '未发车':
			chineseHtml = '3';
			break;
		case '已发车':
			chineseHtml = '4';
			break;
		case '已到达':
			chineseHtml = '5';
			break;
		case '服务中':
			chineseHtml = '6';
			break;
		case '已完成':
			chineseHtml = '7';
			break;
		case '已取消':
			chineseHtml = '8';
			break;
		case '全部':
			chineseHtml = '';
			break;
	}
	return chineseHtml;
};

// 服务项转中文
const serviceCodeTrans = code => {
	let chineseHtml = '';
	if (code.indexOf('JHDJ') !== -1) {
		chineseHtml = '酒后代驾';
	} else if (code.indexOf('JCJS') !== -1) {
		chineseHtml = '境内机场接送';
	} else if (code.indexOf('JWJS') !== -1) {
		chineseHtml = '境外机场接送';
	} else if (code.indexOf('YCFW') !== -1) {
		chineseHtml = '用车服务';
	} else if (code.indexOf('DBNJ') !== -1) {
		chineseHtml = '代办年检';
	} else if (code.indexOf('DLJY') !== -1) {
		chineseHtml = '道路救援';
	} else if (code.indexOf('SWDJ') !== -1) {
		chineseHtml = '商务代驾';
	}
	return chineseHtml;
};

// blob to dataURL
const blobToDataURL = blob => {
	const $q = injector.get('$q');
	let deferred = $q.defer();
	let reader = new FileReader();
	reader.onload = e => { deferred.resolve(e.target.result); };
	reader.readAsDataURL(blob);
	return deferred.promise;
};

// file to dataURL
const fileToDataURL = (file, scope) => {
	const $q = injector.get('$q');
	let deferred = $q.defer();
	let reader = new FileReader();
	reader.onload = function (e) { deferred.resolve(e.target.result); };
	reader.onprogress = event => {
		scope.$broadcast('fileProgress', {
			total: event.total,
			loaded: event.loaded
		});
	};
	reader.readAsDataURL(file);
	return deferred.promise;
};

export { timestampToDate, goBack, setCookie, getCookie, delCookie, getButtonList, orderCheckStateTrans, orderStateTrans, blobToDataURL, fileToDataURL, serviceCodeTrans, getZtreeString };
